<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card>
        <div class="addBtn" style="text-align:right;margin-bottom:20px;">
          <el-button type="primary" size="small" @click="addPermission(1, '0')">
            添加权限
          </el-button>
        </div>
        <el-table
          v-loading="loadingTable"
          :data="permissionList"
          style="width: 100%"
          border
          row-key="id"
        >
          <el-table-column prop="name" label="名称"> </el-table-column>
          <el-table-column prop="code" label="标识"> </el-table-column>
          <el-table-column prop="description" label="描述"> </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="{ row }">
              <!-- row.type !== 1  -->
              <el-button
                :disabled="!$checkPermission('POINT-USER-ADD')"
                type="text"
                @click="addPermission(2, row.id)"
              >
                添加权限点
              </el-button>
              <el-button
                :disabled="!$checkPermission('POINT-USER-UPDATE')"
                type="text"
                @click="editPermission(row.id)"
              >
                查看
              </el-button>
              <el-button
                :disabled="!$checkPermission('POINT-USER-DEL')"
                type="text"
                @click="delPermission(row.id)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>

    <!-- 添加and编辑 权限点的弹框 -->
    <el-dialog
      :title="showTitle"
      :visible.sync="showPermissionDialog"
      width="width"
      @close="closePermissionDialog"
    >
      <el-form
        ref="permissionForm"
        v-loading="loadingForm"
        :model="permissionForm"
        label-width="80px"
      >
        <el-form-item label="权限名称">
          <el-input v-model="permissionForm.name" placeholder="请输入权限名称"></el-input>
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="permissionForm.code" placeholder="请输入权限标识"></el-input>
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input
            v-model="permissionForm.description"
            placeholder="请输入权限描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="permissionForm.enVisible"
            active-text="启用"
            inactive-text="未启用"
            active-value="0"
            inactive-value="1"
          />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="showPermissionDialog = false">取 消</el-button>
        <el-button type="primary" @click="clickSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- /添加and编辑 权限点的弹框 -->
  </div>
</template>

<script>
import {
  reqAddPermission,
  reqDelPermission,
  reqGetPermission,
  reqGetPermissionDetail,
  reqEditPermission,
} from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'

export default {
  name: 'Permission',
  data() {
    return {
      // 权限列表
      permissionList: [],
      showPermissionDialog: false, // 添加权限弹出层
      permissionForm: {
        enVisible: '1', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '', // 添加到哪个节点下
      },
      loadingTable: false, // 表格loading
      loadingForm: false, // 弹出层loading
    }
  },

  computed: {
    // 根据是否有id计算显示标题是添加还是编辑
    showTitle() {
      if (this.permissionForm.id) {
        return '编辑权限'
      } else {
        return '添加权限'
      }
    },
  },

  created() {
    // 调用获取权限点的方法
    this.getPermission()
    // console.log(this.$checkPermission('POINT-USER-ADD'))
  },
  methods: {
    // 一、获取权限点
    async getPermission() {
      this.loadingTable = true
      const data = await reqGetPermission()
      // console.log(data)
      // 转成树形结构
      this.permissionList = tranListToTreeData(data, '0')
      this.$message.success('获取权限列表成功')
      this.loadingTable = false
    },

    // 二、添加权限点
    addPermission(type, pid) {
      // console.log(type, pid)
      // 1.显示弹层
      this.showPermissionDialog = true
      this.permissionForm.type = type
      this.permissionForm.pid = pid
    },

    // 三、点击确认添加或修改权限
    async clickSubmit() {
      // 1.名称或标识符不能为空
      if (this.permissionForm.name === '' || this.permissionForm.code === '') {
        return this.$message.error('名称或标识符不能为空')
      }
      // 2.根据是否有id,判断是发送添加请求还是编辑请求
      if (this.permissionForm.id) {
        // 编辑请求
        await reqEditPermission(this.permissionForm)
        this.$message.success('修改成功')
      } else {
        // 添加请求
        await reqAddPermission(this.permissionForm)
        this.$message.success('添加成功')
      }
      this.getPermission()
      this.showPermissionDialog = false
    },

    // 四、删除权限点
    async delPermission(id) {
      try {
        await this.$confirm('确定要删除吗?', '温馨提示', { type: 'warning' })
        await reqDelPermission(id)
        this.$message.success('删除成功')
        this.getPermission()
      } catch {
        this.$message.info('已取消')
      }
    },

    // 五、关闭弹出层触发
    closePermissionDialog() {
      this.permissionForm = {}
    },

    // 六、查看权限点
    async editPermission(id) {
      this.showPermissionDialog = true
      this.loadingForm = true
      // 根据id查询
      const data = await reqGetPermissionDetail(id)
      // console.log(data)
      this.permissionForm = data
      this.loadingForm = false
    },
  },
}
</script>

<style></style>
